
    /**  
    * @Title: HttpRequestHandler.java
    * @Package com.third.netty.coder.handler
    * @Description: 
    * @author likai
    * @date 2018年1月28日
    * @version V1.0  
    */
    
package com.plug.base.netty;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;  
  

    /**
    * @ClassName: HttpRequestHandler
    * @Description: 过滤请求，WebSocket继续处理，http则过滤
    * @author likai
    * @date 2018年1月28日
    *
    */
@Sharable
public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest>{
	public static Logger log = LoggerFactory.getLogger(HttpRequestHandler.class);
    private final String wsUri;  
    
    public HttpRequestHandler(String wsUri) {  
        this.wsUri = wsUri;  
    }  
  
    @Override  
    public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {  
        if (wsUri.equalsIgnoreCase(request.uri())) {  
            //如果是WebSocket请求，则保留数据并传递到下一个ChannelHandler  
            ctx.fireChannelRead(request.retain());  
        } else {  
        	log.info("----非"+wsUri+"下请求----");
           
            ChannelFuture future;  
            HttpResponse response = new DefaultHttpResponse(request.protocolVersion(), HttpResponseStatus.OK);  
            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");  

            future= ctx.write(response);  
            future.addListener(ChannelFutureListener.CLOSE);  

        }  
  
    }  

  
    @Override  
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {  
        cause.printStackTrace();  
        ctx.close();  
    }  
}
